<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :table-loading="loading"
               :permission="permissionList"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :page="page"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  remove,
  update,
  add
} from "@/api/base/store/storeAddress";
import { mapGetters } from "vuex";

export default {
  data () {
    return {
      form: {},
      query: {},
      loading: true,
      rolesTree: [],
      rolesKeys: [],
      box: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        size: 'mini',
        excelBtn: true,
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        rowKey: 'id',
        height: 'auto',
        // calcHeight: 380,
        dialogType: 'dialog',
        menuWidth: 300,
        column: [
          {
            label: "添加时间",
            prop: "addTime",
            fixed: true,
            rules: [{
              required: true,
              message: "请输入添加时间",
              trigger: "blur"
            }]
          },
          {
            label: "删除状态 0:正常  1:删除",
            prop: "deleteStatus",
            rules: [{
              required: true,
              message: "请输入删除状态 0:正常  1:删除",
              trigger: "blur"
            }]
          },
          {
            label: "详细地址",
            prop: "areaInfo",
            rules: [{
              required: true,
              message: "请输入详细地址",
              trigger: "blur"
            }]
          },
          {
            label: "邮编",
            prop: "zip",
            rules: [{
              required: true,
              message: "请输入邮编",
              trigger: "blur"
            }]
          },
          {
            label: "地区信息外键  area表县或者区id",
            prop: "areaId",
            rules: [{
              required: true,
              message: "请输入地区信息外键  area表县或者区id",
              trigger: "blur"
            }]
          },
          {
            label: "店铺id",
            prop: "storeId",
            rules: [{
              required: true,
              message: "请输入店铺id",
              trigger: "blur"
            }]
          },
          {
            label: "地址类型(1:公司地址;2:寄件地址;3:收件地址)",
            prop: "addressType",
            rules: [{
              required: true,
              message: "请输入地址类型(1:公司地址;2:寄件地址;3:收件地址)",
              trigger: "blur"
            }]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList () {
      return {
        addBtn: this.vaildData(this.permission.mgr_add, false),
        viewBtn: this.vaildData(this.permission.mgr_view, false),
        delBtn: this.vaildData(this.permission.mgr_delete, false),
        editBtn: this.vaildData(this.permission.mgr_edit, false)
      };
    },
  },
  methods: {
    rowSave (row, loading, done) {
      row.position = row.positionIds;
      row.deptName = row.$deptId;
      row.repassword = row.password;
      add(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();
        ;
      });
    },
    rowUpdate (row, index, loading, done) {
      row.position = row.positionIds;
      update(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();
        ;
      });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange (params,done) {
      this.query = params;
      this.onLoad(this.page, params,done);
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange () {
      this.onLoad(this.page);
    },
    onLoad (page, params = {},done) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        if(done){
          done();
        }
        this.loading = false;
        const data = res.data;
        this.page.total = data.count;
        this.data = data.data;
      });
    }
  }
};
</script>

<style>
</style>
